﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
<head>

  <meta http-equiv="Content-Language" content="en-us">



  <title>IupSetAttribute</title>
  <link rel="stylesheet" type="text/css" href="../../style.css">

  <style type="text/css">
.style1 {
	color: #FF0000;
}
  .style2 {
	background-color: #CEE7FF;
}
  </style>
</head>


<body>


<h2>IupSetAttribute</h2>

<p>Sets an interface 
  element attribute. See also the <a href="../attrib_guide.html">Attributes Guide</a> 
section.</p>

<h3>
Parameters/Return</h3>

<pre>void IupSetAttribute(Ihandle *<strong>ih</strong>, const char *<strong>name</strong>, const char *<strong>value</strong>); [in C]
void IupSetStrAttribute(Ihandle *<strong>ih</strong>, const char *<strong>name</strong>, const char *<strong>value</strong>); [in C]<br>iup.SetAttribute(<strong>ih</strong>: ihandle, <strong>name</strong>: string, <strong>value</strong>: any) [in Lua] 

void IupSetAttributeId(Ihandle *<strong>ih</strong>, const char *<strong>name</strong>, int <strong>id</strong>, const char *<strong>value</strong>); [in C]
void IupSetStrAttributeId(Ihandle *<strong>ih</strong>, const char *<strong>name</strong>, int <strong>id</strong>, const char *<strong>value</strong>); [in C]<br>iup.SetAttributeId(<strong>ih</strong>: ihandle, <strong>name</strong>: string, <strong>id</strong>: number, <strong>value</strong>: any) [in Lua] 

void IupSetAttributeId2(Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, const char* <strong>value</strong>); [in C]
void IupSetStrAttributeId2(Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, const char* <strong>value</strong>); [in C]
iup.SetAttributeId2(<strong>ih</strong>: ihandle, <strong>name</strong>: string, <strong>lin, col</strong>: number, <strong>value</strong>: any) [in Lua]</pre>

<p><b>ih</b>: Identifier of the interface element. If
  NULL will set in the global environment.<br>

<b>name</b>: name of the attribute.<br>
<strong>id, lin, col</strong>: used when the attribute has additional ids.<br>

<b>value</b>: value of the attribute. If NULL (nil in Lua), the default value 
will be used.</p>


<h3>Utility Functions </h3>
<p>These functions can also be used to set attributes from the element:</p>
<pre>void IupSetStrf  (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, const char* <strong>format</strong>, ...);
void IupSetStrfV (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, const char* <strong>format</strong>, va_list <strong>arglist</strong>);
void IupSetInt   (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>value</strong>);
void IupSetFloat (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, float <strong>value</strong>);
void IupSetDouble(Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, double <strong>value</strong>);
void IupSetRGB   (Ihandle *<strong>ih</strong>, const char* <strong>name</strong>, unsigned char <strong>r</strong>, unsigned char <strong>g</strong>, unsigned char <strong>b</strong>);
void IupSetRGBA  (Ihandle *<strong>ih</strong>, const char* <strong>name</strong>, unsigned char <strong>r</strong>, unsigned char <strong>g</strong>, unsigned char <strong>b</strong>, unsigned char <strong>a</strong>); (since 3.29)

void IupSetStrfId  (Ihandle *<strong>ih</strong>, const char* <strong>name</strong>, int <strong>id</strong>, const char* <strong>format</strong>, ...);
void IupSetStrfIdV (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>id</strong>, const char* <strong>format</strong>, va_list <strong>arglist</strong>);
void IupSetIntId   (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>id</strong>, int <strong>value</strong>);
void IupSetFloatId (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>id</strong>, float <strong>value</strong>);
void IupSetDoubleId(Ihandle* <strong>ih</strong>, const char* <strong>name, </strong>int<strong> id, </strong>double <strong>value</strong>);
void IupSetRGBId   (Ihandle *<strong>ih</strong>, const char* <strong>name, </strong>int<strong> id</strong>, unsigned char <strong>r</strong>, unsigned char <strong>g</strong>, unsigned char <strong>b</strong>);

void IupSetStrfId2  (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, const char* <strong>format</strong>, ...);
void IupSetStrfId2V (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, const char* <strong>format</strong>, va_list <strong>arglist</strong>);
void IupSetIntId2   (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, int <strong>value</strong>);
void IupSetFloatId2 (Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, float <strong>value</strong>);
void IupSetDoubleId2(Ihandle* <strong>ih</strong>, const char* <strong>name</strong>, int <strong>lin</strong>, int <strong>col</strong>, double <strong>value</strong>);
void IupSetRGBId2   (Ihandle *<strong>ih</strong>, const char* <strong>name, </strong>int<strong> lin</strong>, int <strong>col</strong>, unsigned char <strong>r</strong>, unsigned char <strong>g</strong>, unsigned char <strong>b</strong>);

[There is no equivalent in Lua]</pre>
<p><strong>IupSetStrf</strong>* functions (old <strong>IupSetfAttribute</strong>) 
uses the same format specification as the 
<b>sprintf</b> function in C. This function is very useful when several values must be combined into one 
string. When passing float values, it uses the format &quot;%.9g&quot; to maximize precision. 
When passing double values, it uses the format "%.18g&quot; to maximize precision.</p>
<p>All the utility functions use the <strong>IupSetStrAttribute</strong>* 
functions internally.<br>
</p>
<h3>Notes</h3>
<p>See the <a href="../attrib_guide.html">Attributes Guide</a> for more details.</p>
<p><strong>IupSetAttribute</strong> can store only constant strings (like 
&quot;Title&quot;, &quot;30&quot;, etc) or application pointers. The given value is not duplicated 
as a string, only a reference is stored. Therefore, you can store application 
custom attributes, such as a context structure to be used in a callback.</p>
<p><strong>IupSetStrAttribute</strong> (old <strong>IupStoreAttribute</strong>) 
can only store strings. The given string value will be duplicated internally.&nbsp;</p>
<p><strong>Id</strong> based attributes are always non inheritable, so all
<strong>IupSet*Id</strong> functions will not propagate the attribute to the 
children. <strong>Ids</strong> are usually non negative values (id &gt;= 0), with a 
few exceptions. </p>

<h3>Examples</h3>

<p>A very common mistake when using <strong>IupSetAttribute</strong> is to use local string arrays to set attributes. For ex:</p>

<pre>char value[30];<br>sprintf(value, "CODE - %d", i);<br>IupSetAttribute(dlg, "BADEXAMPLE", value)  // <span class="style1"><strong>WRONG</strong></span>  (value pointer will be internally stored, <br>                                           //         but its memory will be released at the end of this scope)
                                           // a common bad practice is to declare value as static<br>                                           // Use <strong>IupSetStrAttribute</strong> in this case</pre>

<pre>char *value = malloc(30);<br>sprintf(value, "%d", i);<br>IupSetAttribute(dlg, "EXAMPLE", value)     // correct  (but to avoid memory leaks you should free the pointer<br>                                                        after the dialog has been destroyed)</pre>

<pre>IupSetAttribute(dlg, "VISIBLE", "YES")     // correct (constant values still exists after this scope)
IupSetAttribute(text, "VALUE", "Hello!");
IupSetAttribute(indicator, "VALUE", "ON");</pre>

<pre>char attrib[30];<br>sprintf(attrib, "MY ITEM (%d)", i);<br>IupSetAttribute(dlg, attrib, "Test")       // correct (attribute names are always internally duplicated)</pre>

<pre>struct{<br>  int x;<br>  int y;<br>} myData;<br><br>IupSetAttribute(text, "myData", (char*)&amp;myData);  // <span class="style1"><strong>WRONG</strong></span>, will work only i<span class="style2">f</span> myData i<span class="style2">s</span> a global variable.</pre>

<pre>struct myData* mydata = malloc(sizeof(struct myData));<br>IupSetAttribute(dlg, "MYDATA", (char*)mydata);    // correct (unknown attributes will be stored as pointers)</pre>

<p>Defines a radio’s initial value:</p>

<pre>Ihandle *portrait = IupToggle("Portrait" , NULL);<br>Ihandle *landscape = IupToggle("landscape" , NULL);<br>Ihandle *box = IupVbox(portrait, IupFill(),landscape, NULL);<br>Ihandle *mode = IupRadio(box);<br>IupSetHandle("landscape", landscape); /* associates a name to initialize the radio */<br><strong>IupSetAttribute</strong>(mode, "VALUE", "landscape"); /* defines the radio&rsquo;s initial value */</pre>

<h3>See Also</h3>

<p><a href="iupgetattribute.html">IupGetAttribute</a>, 
<a href="iupsetattributes.html">IupSetAttributes</a>, 
<a href="iupgetattributes.html">IupGetAttributes</a>,
<a href="iupsetglobal.html">IupSetGlobal</a>, <a href="iupgetglobal.html">
IupGetGlobal</a></p>


</body>
</html>
